// components/approvalCom/approvalCom.js
Component({
  /**
   * 组件的属性列表
   */
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      console.log(this.properties.prop)
      console.log(this.properties.isOwner)
      this.setData({
        isOwner:this.properties.isOwner
      })
      switch (this.properties.prop.isAgree) {
        case 0:
          this.setData({
            showText: "待定",
            allowEdit: true
          })
          break;
        case 1:
          this.setData({
            showText: "通过",
            allowEdit: false
          })
          break;
        case 2:
          this.setData({
            showText: "忽略",
            allowEdit: true
          })
          break;
        case -1:
          this.setData({
            showText: "拒绝",
            allowEdit: false
          })
          break;
      }
      this.setData({
        content: this.properties.prop
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  properties: {
    prop: {
      "visitorName": "1",
      "visitorID": "11",
    },
    isOwner:true
  },

  /**
   * 组件的初始数据
   */
  data: {
    showIndex: 0,
    showPopup: false,
    content: {},
    columns: ['待定', '通过', '拒绝', '忽略'],
    showText: "待定",
    allowEdit: true,
    sendStatus: 0,
    isOwner:true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    panel: function (e) {
      if (e.currentTarget.dataset.index != this.data.showIndex) {
        this.setData({
          showIndex: e.currentTarget.dataset.index
        })
      } else {
        this.setData({
          showIndex: 0
        })
      }
    },
    showPopup: function () {
      this.setData({
        showPopup: true
      })
    },
    closePopup: function () {
      this.setData({
        showPopup: false
      })
    },
    onCancel: function () {
      this.setData({
        showPopup: false
      })
    },
    onConfirm: function (event) {
      console.log("确认事件")
      console.log(event.detail.value)
      this.setData({
        showPopup: false,
        showText: event.detail.value
      })
      switch (event.detail.value) {
        case '待定':
          this.setData({
            sendStatus: 0
          })
          break;
        case '通过':
          this.setData({
            sendStatus: 1
          })
          break;
        case '拒绝':
          this.setData({
            sendStatus: -1
          })
          break;
        case '忽略':
          this.setData({
            sendStatus: 2
          })
          break;
      }
      let _that = this
      wx.request({
        url: 'http://localhost/Wx/AnymousApprove?id=' + _that.data.content.id + '&isAgree=' + _that.data.sendStatus,
        method: "POST",
        success(res) {
          console.log(res)
          if (res.data.code == 200) {
            wx.showModal({
              title: '提示',
              content: '审批成功',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
            if ((_that.data.sendStatus == 1) || (_that.data.sendStatus == -1)) {
              _that.setData({
                allowEdit: false
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              content: '审批错误',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }

  }
})